package models

import (
	_ "time"

	orm "go-papers/database"
)

type OrganShare struct {
	OrderId int `json:"orderId" gorm:"type:int(11);primary_key"` //
	OrganId int `json:"organId" gorm:"type:int(11);"`            // 机构id

}

// orderStatus
//订单状态1 进行中（创建）
//支付状态
//5  待付款     用户下单后 未完成支付支付完成
//10 已支付 （- 订单可以申请退款-）
//20 已确认 （用户确认订单- 订单可以申请退款  / 或者 系统15天自动确认）   -- （ 进行分账给机构 ）退款状态
//60 用户申请退款/待核实  （前台触发 微信端用户点击 ）
//65 审核不通过/退款申请失败（订单可以申请退款）   --（后台点击退款审核 不通过）
//70 退款申请成功/ 待退款                                             -- （后台点击退款审核通过）
//80 全部退款 完成 （此动作 后台触发 将进行 微信订单退款操作）订单结束状态
// 90  已完成 （此状态下 订单不能申请退款  / 订单已经完成排课 且到期）
// 91 已关闭 （/已退款完成 周期7天完成关闭）

// closeReson: "212"
//id: 1
//orderId: "1"
//orderPrice: 212
//refundPrice: 212
type OrderClose struct {
	OrderId     int     `json:"orderId" gorm:"type:int(11);primary_key"` //
	CloseReason string  `json:"closeReason" gorm:"type:datetime;"`       // 订单关闭原因
	OrderPrice  float64 `json:"orderPrice" gorm:"type:decimal(10,2);"`   // 订单有效金额 --refundPrice float64
	TotalPrice  float64 `json:"totalPrice" gorm:"type:decimal(10,2);"`   // 订单创建总金额
	RefundPrice float64 `json:"refundPrice" gorm:"type:decimal(10,2);"`  // 退款金额
	RefundTime  string  `json:"refundTime" gorm:"type:datetime;"`        // 关闭时间
	OrderStatus string  `json:"orderStatus" gorm:"type:tinyint(1);"`
}

type Order struct {
	OrderId         int    `json:"orderId" gorm:"type:int(11);primary_key"` //
	OrderNo         string `json:"orderNo" gorm:"type:varchar(128);"`       // 订单编号
	OrderName       string `json:"orderName" gorm:"type:varchar(255);"`     // 订单标题
	OrderStatus     string `json:"orderStatus" gorm:"type:tinyint(1);"`     //
	OrderStatusName string `json:"orderStatusName" gorm:"-"`

	OrderPrice       float64 `json:"orderPrice" gorm:"type:decimal(10,2);"`       // 订单有效金额 --refundPrice float64
	TotalPrice       float64 `json:"totalPrice" gorm:"type:decimal(10,2);"`       // 订单创建总金额
	ShareOrganPrice  float64 `json:"shareOrganPrice" gorm:"type:decimal(10,2);"`  // 订单 机构获得课程费用 去除佣金
	ShareOrganRefund float64 `json:"shareOrganRefund" gorm:"type:decimal(10,2);"` // 订单 机构退款金额 不含佣金
	RefundPrice      float64 `json:"refundPrice" gorm:"type:decimal(10,2);"`      // 退款金额
	RefundReason     string  `json:"refundReason" gorm:"type:varchar(255);"`      // 退款金额
	RefundNo         string  `json:"refundNo" gorm:"type:varchar(255);"`          // 退款金额
	ShareCfg         string  `json:"shareCfg" gorm:"type:text;"`                  // 退款金额

	MemberId      int    `json:"memberId" gorm:"type:int(11);"`             // 购买的会员id
	MemberMomId   int    `json:"memberMomId" gorm:"type:int(11);"`          // 购买的会员 管家妈妈 社区达人id
	MemberMomName string `json:"member_mom_name" gorm:"type:varchar(255);"` // 购买的会员id 管家妈妈名称
	OpenId        string `json:"open_id" gorm:"type:varchar(255);"`         // 购买的会员id 管家妈妈名称
	MemberName    string `json:"memberName" gorm:"type:varchar(255);"`      // 购买的会员昵称
	MemberPhone   string `json:"memberPhone" gorm:"type:varchar(255);"`     // 购买会员的手机号
	PayAccount    string `json:"payAccount" gorm:"type:varchar(255);"`      // 支付账户

	OrganPayType    string `json:"organPayType" gorm:"type:varchar(255);"`    // 机构支付账户类型
	OrganPayAccount string `json:"organPayAccount" gorm:"type:varchar(255);"` // 支付账户 商户号

	PayTradeNo        string      `json:"payTradeNo" gorm:"type:varchar(255);"`         // 支付账户
	CourseCover       string      `json:"courseCover" gorm:"type:varchar(255);"`        // 课程封面
	CourseId          int         `json:"courseId" gorm:"type:int(11);"`                // 课程id
	OrganId           int         `json:"organId" gorm:"type:int(11);"`                 // 机构id
	OrganMomId        int         `json:"organMomId" gorm:"type:int(11);"`              // 机构id
	OrganMomName      string      `json:"organMomName" gorm:"type:varchar(255);"`       // 机构名称
	OrganName         string      `json:"organName" gorm:"type:varchar(255);"`          // 机构名称
	OrganTel          string      `json:"organTel" gorm:"-"`                            // 机构名称
	CourseName        string      `json:"courseName" gorm:"type:varchar(255);"`         // 课程名称
	BrandName         string      `json:"brandName" gorm:"type:varchar(255);"`          // 机构名称
	CreateTime        string      `json:"createTime" gorm:"type:datetime;"`             // 创建时间
	SureTime          string      `json:"sureTime" gorm:"type:varchar(255);"`           // 创建时间
	PayType           string      `json:"payType" gorm:"type:varchar(16);"`             // 支付类型 wx ali
	PayNo             string      `json:"payNo" gorm:"type:varchar(255);"`              // 支付单号
	PayTime           string      `json:"payTime" gorm:"type:varchar(255);"`            // 支付时间
	RefundTime        string      `json:"refundTime" gorm:"type:varchar(255);"`         // 关闭时间
	CloseTime         string      `json:"closeTime" gorm:"type:varchar(255);"`          // 订单关闭时间
	IsSure            string      `json:"isSure" gorm:"type:varchar(2);"`               // 订单关闭原因
	IsShare           string      `json:"isShare" gorm:"type:varchar(2);"`              // 订单关闭原因
	ShareNo           string      `json:"shareNo" gorm:"type:varchar(255);"`            // 分账订单号
	ShareAccount      string      `json:"shareAccount" gorm:"type:varchar(255);"`       // 分账订单号
	ShareAccountPrice float64     `json:"shareAccountPrice" gorm:"type:decimal(10,2);"` // 分账订单号
	ShareTime         string      `json:"shareTime" gorm:"type:varchar(32);"`           // 订单关闭原因
	CloseReason       string      `json:"closeReason" gorm:"type:varchar(255);"`        // 订单关闭原因
	CourseInfo        interface{} `json:"courseInfo" gorm:"-"`
	OrganInfo         interface{} `json:"organInfo" gorm:"-"`
	StudentInfo       interface{} `json:"studentInfo" gorm:"-"`
	ShareInfo         interface{} `json:"shareInfo" gorm:"-"`
	StudentAddr       string      `json:"studentAddr" gorm:"-"`
	MemberMomPhone    string      `json:"memberMomPhone" gorm:"-"`

	ShareType   string `json:"shareType" gorm:"-"`   // 类型 1平台与机构  2 社区达人与拓展用户  3 社区达人与拓展机构
	ShareUserId int    `json:"shareUserId" gorm:"-"` // 类型 1平台与机构  2 社区达人与拓展用户  3 社区达人与拓展机构
	ShareStatus string `json:"shareStatus" gorm:"-"` // 分成状态 1 待确认 3 待已确认/待领取  5 已领取

	UpdateStart string `json:"-" gorm:"-"` // 跟新 起始时间
	UpdateEnd   string `json:"-" gorm:"-"` // 跟新 结束时间

	PayTimeStart string `json:"-" gorm:"-"` // 支付 起始时间
	PayTimeEnd   string `json:"-" gorm:"-"` // 支付  结束时间

	ShareTimeMonth string `json:"-" gorm:"-"` // 查询月份

	SureTimeStart string `json:"-" gorm:"-"` // 支付 起始时间
	SureTimeEnd   string `json:"-" gorm:"-"` // 支付  结束时间

	ShareTimeStart string `json:"-" gorm:"-"` // 支付 起始时间
	ShareTimeEnd   string `json:"-" gorm:"-"` // 支付  结束时间

	AddressSearch string `json:"-"  gorm:"-"` //地区搜索
	Address       string `json:"-"  gorm:"-"`
	StatusType    string `json:"-"  gorm:"-"`

	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`

	BaseModel
}

type OrderFiance struct {
	OrderId         int    `json:"orderId" gorm:"type:int(11);primary_key"` //
	OrderNo         string `json:"orderNo" gorm:"type:varchar(128);"`       // 订单编号
	OrderName       string `json:"orderName" gorm:"type:varchar(255);"`     // 订单标题
	OrderStatus     string `json:"orderStatus" gorm:"type:tinyint(1);"`     //
	OrderStatusName string `json:"orderStatusName" gorm:"-"`

	OrderPrice   float64 `json:"orderPrice" gorm:"type:decimal(10,2);"`  // 订单有效金额 --refundPrice float64
	TotalPrice   float64 `json:"totalPrice" gorm:"type:decimal(10,2);"`  // 订单创建总金额
	RefundPrice  float64 `json:"refundPrice" gorm:"type:decimal(10,2);"` // 退款金额
	RefundReason string  `json:"refundReason" gorm:"type:varchar(255);"` // 退款金额
	ShareCfg     string  `json:"shareCfg" gorm:"type:text;"`             // 退款金额

	MemberId       int    `json:"memberId" gorm:"type:int(11);"`             // 购买的会员id
	MemberMomId    int    `json:"memberMomId" gorm:"type:int(11);"`          // 购买的会员 管家妈妈 社区达人id
	MemberMomName  string `json:"member_mom_name" gorm:"type:varchar(255);"` // 购买的会员id 管家妈妈名称
	MemberName     string `json:"memberName" gorm:"type:varchar(255);"`      // 购买的会员昵称
	MemberPhone    string `json:"memberPhone" gorm:"type:varchar(255);"`     // 购买会员的手机号
	PayAccount     string `json:"payAccount" gorm:"type:varchar(255);"`      // 支付账户
	PayTradeNo     string `json:"payTradeNo" gorm:"type:varchar(255);"`      // 支付账户
	CourseCover    string `json:"courseCover" gorm:"type:varchar(255);"`     // 课程封面
	CourseId       int    `json:"courseId" gorm:"-"`                         // 课程id
	OrganId        int    `json:"organId" gorm:"type:int(11);"`              // 机构id
	OrganName      string `json:"organName" gorm:"type:varchar(255);"`       // 机构名称
	OrganMoneyType string `json:"organMoneyType" gorm:"type:varchar(255);"`
	CourseName     string `json:"courseName" gorm:"type:varchar(255);"`  // 课程名称
	BrandName      string `json:"brandName" gorm:"type:varchar(255);"`   // 机构名称
	CreateTime     string `json:"createTime" gorm:"type:datetime;"`      // 创建时间
	PayType        string `json:"payType" gorm:"type:varchar(16);"`      // 支付类型 wx ali
	PayNo          string `json:"payNo" gorm:"type:varchar(255);"`       // 支付单号
	PayTime        string `json:"payTime" gorm:"type:varchar(255);"`     // 支付时间
	RefundTime     string `json:"refundTime" gorm:"type:varchar(255);"`  // 关闭时间
	CloseTime      string `json:"closeTime" gorm:"type:varchar(255);"`   // 订单关闭时间
	CloseReason    string `json:"closeReason" gorm:"type:varchar(255);"` // 订单关闭原因
	StudentAddr    string `json:"studentAddr" gorm:"type:varchar(255);"`

	IsSure              string  `json:"isSure"  gorm:"type:varchar(2);"`
	SureTime            string  `json:"sureTime" gorm:"type:varchar(255);"` // 创建时间
	IsShare             string  `json:"isShare" gorm:"type:varchar(2);"`
	ShareTime           string  `json:"shareTime" gorm:"type:varchar(255);"`
	ShareType           string  `json:"shareType" gorm:"type:varchar(255);"`            // 类型 1平台与机构  2 社区达人与拓展用户  3 社区达人与拓展机构
	ShareStatus         string  `json:"shareStatus" gorm:"type:varchar(255);"`          // 分成状态 1  待确认  5 已确认/已打款   10 待退回 20 已退回  50 已取消/关闭
	ShareMomOrganPrice  float64 `json:"shareMomOrganPrice" gorm:"type:decimal(10,2);"`  // 佣金 社区达人 与机构
	ShareMomMemberPrice float64 `json:"shareMomMemberPrice" gorm:"type:decimal(10,2);"` // 佣金 社区达人 与用户

	UpdateStart  string `json:"-" gorm:"-"` // 跟新 起始时间
	UpdateEnd    string `json:"-" gorm:"-"` // 跟新 结束时间
	PayTimeStart string `json:"-" gorm:"-"` // 支付 起始时间
	PayTimeEnd   string `json:"-" gorm:"-"` // 支付  结束时间

	ShareTimeStart string `json:"-" gorm:"-"` // 支付 起始时间
	ShareTimeEnd   string `json:"-" gorm:"-"` // 支付  结束时间

	AddressSearch string `json:"-"  gorm:"-"` //地区搜索
	Address       string `json:"-"  gorm:"-"`
	StatusType    string `json:"-"  gorm:"-"`

	StudentAddress string  `json:"studentAddress"  gorm:"-"`
	MomSharePrice  float64 `json:"momSharePrice"  gorm:"-"`
}

type OrderShareList struct {
	OrderId         int    `json:"orderId" gorm:"type:int(11);primary_key"` //
	OrderNo         string `json:"orderNo" gorm:"type:varchar(128);"`       // 订单编号
	OrderName       string `json:"orderName" gorm:"type:varchar(255);"`     // 订单标题
	OrderStatus     string `json:"orderStatus" gorm:"type:tinyint(1);"`     //
	OrderStatusName string `json:"orderStatusName" gorm:"-"`                //

	OrderPrice  float64 `json:"orderPrice" gorm:"type:decimal(10,2);"`  // 订单有效金额 --refundPrice float64
	TotalPrice  float64 `json:"totalPrice" gorm:"type:decimal(10,2);"`  // 订单创建总金额
	RefundPrice float64 `json:"refundPrice" gorm:"type:decimal(10,2);"` // 退款金额

	MemberId      int    `json:"memberId" gorm:"type:int(11);"`             // 购买的会员id
	MemberMomId   int    `json:"memberMomId" gorm:"type:int(11);"`          // 购买的会员 管家妈妈 社区达人id
	MemberMomName string `json:"member_mom_name" gorm:"type:varchar(255);"` // 购买的会员id 管家妈妈名称
	MemberName    string `json:"memberName" gorm:"type:varchar(255);"`      // 购买的会员昵称
	MemberPhone   string `json:"memberPhone" gorm:"type:varchar(255);"`     // 购买会员的手机号
	PayAccount    string `json:"payAccount" gorm:"type:varchar(255);"`      // 支付账户
	CourseCover   string `json:"courseCover" gorm:"type:varchar(255);"`     // 课程封面
	CourseId      int    `json:"courseId" gorm:"-"`                         // 课程id
	OrganId       int    `json:"organId" gorm:"type:int(11);"`              // 机构id
	ShareId       int    `json:"shareId" gorm:"type:int(11);"`              // 机构id
	OrganName     string `json:"organName" gorm:"type:varchar(255);"`       // 机构名称
	CourseName    string `json:"courseName" gorm:"type:varchar(255);"`      // 课程名称
	BrandName     string `json:"brandName" gorm:"type:varchar(255);"`       // 机构名称
	CreateTime    string `json:"createTime" gorm:"type:datetime;"`          // 创建时间
	PayType       string `json:"payType" gorm:"type:varchar(16);"`          // 支付类型 wx ali
	PayNo         string `json:"payNo" gorm:"type:varchar(255);"`           // 支付单号
	PayTime       string `json:"payTime" gorm:"type:varchar(255);"`         // 支付时间
	RefundTime    string `json:"refundTime" gorm:"type:varchar(255);"`      // 关闭时间
	CloseTime     string `json:"closeTime" gorm:"type:varchar(255);"`       // 订单关闭时间
	CloseReason   string `json:"closeReason" gorm:"type:varchar(255);"`     // 订单关闭原因
	SureTime      string `json:"sureTime" gorm:"type:varchar(255);"`        // 创建时间

	CourseInfo  interface{} `json:"courseInfo" gorm:"-"`
	OrganInfo   interface{} `json:"organInfo" gorm:"-"`
	StudentInfo interface{} `json:"studentInfo" gorm:"-"`

	ShareType   string  `json:"shareType" gorm:"-"`   // 类型 1平台与机构  2 社区达人与拓展用户  3 社区达人与拓展机构
	ShareStatus string  `json:"shareStatus" gorm:"-"` //
	ShareName   string  `json:"shareName" gorm:"-"`   //
	SharePrice  float64 `json:"sharePrice" gorm:"-"`
	ShareRate   float64 `json:"shareRate" gorm:"-"`
	ShareTime   string  `json:"shareTime" gorm:"-"`
	ShareDesc   string  `json:"shareDesc" gorm:"-"`
	ShareInfo   string  ` json:"shareInfo" gorm:"-"`

	OrderShareId        int     `json:"orderShareId" gorm:"-"`        //
	ShareUserId         int     `json:"shareUserId" gorm:"-"`         //
	ShareMomOrganPrice  float64 `json:"shareMomOrganPrice" gorm:"-"`  //  签约机构达人佣金  shareMomOrganPrice
	ShareMomOrganRate   float64 `json:"shareMomOrganRate" gorm:"-"`   //  签约机构达人佣金占比 shareMomOrganRate
	ShareMomMemberPrice float64 `json:"shareMomMemberPrice" gorm:"-"` //  拓展用户达人佣金 shareMomMemberPrice
	ShareMomMemberRate  float64 `json:"shareMomMemberRate" gorm:"-"`  //  拓展用户达人佣金占比  shareMomMemberRate
	ShareOrganPrice     float64 `json:"shareOrganPrice" gorm:"-"`     //  机构课程费用   shareOrganPrice
	ShareOrganRate      float64 `json:"shareOrganRate" gorm:"-"`      //  机构课程费用占比 shareOrganRate
	ShareMomLevel       string  `json:"shareMomLevel" gorm:"-"`       //  拓展用户达人 当前的分佣金 等级level

	UpdateStart string `json:"-" gorm:"-"` // 跟新 起始时间
	UpdateEnd   string `json:"-" gorm:"-"` // 跟新 结束时间

	PayTimeStart string `json:"-" gorm:"-"` // 支付 起始时间
	PayTimeEnd   string `json:"-" gorm:"-"` // 支付  结束时间

	ShareTimeStart string `json:"-" gorm:"-"` // 分佣金确认  起始时间
	ShareTimeEnd   string `json:"-" gorm:"-"` // 分佣金确认  结束时间

	AddressSearch string `json:"-"  gorm:"-"` //地区搜索
	Address       string `json:"-"  gorm:"-"`
	StatusType    string `json:"-"  gorm:"-"`

	DataScope string `json:"dataScope" gorm:"-"`
	Params    string `json:"params"  gorm:"-"`

	BaseModel
}

func (Order) TableName() string {
	return "order"
}

// 创建Order
func (e *Order) Create() (Order, error) {
	var doc Order
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

func (e *Order) GetByID(id int) (Order, error) {
	var doc Order
	table := orm.Eloquent.Table(e.TableName())
	table = table.Where("order_id = ?", id)
	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *Order) GetByOrderNo() (Order, error) {
	var doc Order
	table := orm.Eloquent.Table(e.TableName())
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.OrderNo != "" {
		table = table.Where("order_no = ?", e.OrderNo)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}

	var organ Organ
	organInfo, _ := organ.GetByID(doc.OrganId)
	doc.OrganName = organInfo.OrganName
	doc.OrganTel = organInfo.OrganTel

	var member Member
	mm, _ := member.GetByID(doc.MemberMomId)
	doc.MemberMomPhone = mm.Phone
	doc.MemberMomName = mm.RealName
	return doc, nil
}

// 获取Order
func (e *Order) Get() (Order, error) {
	var doc Order
	table := orm.Eloquent.Table(e.TableName())
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.OrderNo != "" {
		table = table.Where("order_no like ?", "%"+e.OrderNo+"%")
	}
	if e.OrderName != "" {
		table = table.Where("order_name like ?", "%"+e.OrderName+"%")
	}
	if e.OrderStatus != "" {
		table = table.Where("order_status = ?", e.OrderStatus)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}

	//MemberMomId
	if e.MemberMomId != 0 {
		table = table.Where("member_mom_id = ?", e.MemberMomId)
	}
	if e.MemberPhone != "" {
		table = table.Where("member_phone like ?", "%"+e.MemberPhone+"%")
	}
	if e.OrganId != 0 {
		table = table.Where("organ_id = ?", e.OrganId)
	}
	if e.PayType != "" {
		table = table.Where("pay_type = ?", e.PayType)
	}
	if e.PayNo != "" {
		table = table.Where("pay_no like ?", "%"+e.PayNo+"%")
	}
	if e.PayTime != "" {
		table = table.Where("pay_time >= ?", e.PayTime)
	}

	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("updated_at <= ?", e.UpdateEnd)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 财务分红列表 GetSharePage
func (e *Order) GetGatewaySharePage(pageSize int, pageIndex int) ([]OrderShareList, int, error) {
	var doc []OrderShareList

	table := orm.Eloquent.Select(" `order`.* , `order_share`.* ").Table(e.TableName())
	table = table.Joins("inner join `order_share` on `order_share`.order_id = `order`.order_id")

	if e.ShareType != "" {
		table = table.Where("`order_share`.share_type = ?", e.ShareType)
	}
	if e.ShareStatus != "" {
		table = table.Where("`order_share`.share_status = ?", e.ShareStatus)
	}
	if e.ShareUserId != 0 {
		table = table.Where("`order_share`.share_user_id = ?", e.ShareUserId)
	}

	if e.ShareTimeMonth != "" {
		table = table.Where("`order_share`.share_time >= ?", e.ShareTimeMonth+"-00 00:00:00")
		table = table.Where("`order_share`.share_time <= ?", e.ShareTimeMonth+"-31 23:59:59")
	}

	if e.BrandName != "" {
		table = table.Where("`order`.brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.OrganName != "" {
		table = table.Where("`order`.organ_name like ?", "%"+e.OrganName+"%")
	}

	if e.OrderStatus != "" {
		table = table.Where("`order`.order_status = ?", e.OrderStatus)
	}

	//支付时间
	if e.PayTimeStart != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTimeStart)
	}
	if e.PayTimeEnd != "" {
		table = table.Where("`order`.pay_time <= ?", e.PayTimeEnd)
	}

	var count int
	if err := table.Order("`order`.order_id desc", true).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`order`.`deleted_at` IS NULL").Count(&count)

	var courseIn Course
	var organIn Organ
	//var studentIn OrderStudent
	//var shareIn OrderShare
	//处理订单信息
	for i := 0; i < len(doc); i++ {
		ii := doc[i]
		//获取课程信息
		doc[i].CourseInfo, _ = courseIn.GetById(ii.CourseId)

		//获取机构信息
		doc[i].OrganInfo, _ = organIn.GetByID(ii.OrganId)

	}

	return doc, count, nil
}

// 财务分红列表 GetSharePage
func (e *Order) GetSharePageFiance(pageSize int, pageIndex int) ([]OrderShareList, int, error) {
	var doc []OrderShareList

	table := orm.Eloquent.Select(" `order`.* , `order_share`.* ").Table(e.TableName())
	table = table.Joins("inner join `order_share` on `order_share`.order_id = `order`.order_id")

	//使用 课程id
	if e.CourseId != 0 || e.CourseName != "" {
		if e.CourseId != 0 {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id and `order_course`.course_id = ?", e.CourseId)
		} else if e.CourseName != "" {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id ")
			table = table.Where("`order_course`.course_name like ?", "%"+e.CourseName+"%")
		}
	}

	table = table.Joins("inner join `order_student` on `order_student`.order_id = `order`.order_id ")

	//使用到 order_student
	if e.AddressSearch != "" || e.Address != "" {

		//所在地区
		if e.AddressSearch != "" {
			table = table.Where("`order_student`.student_addr like ?", "%"+e.AddressSearch+"%")
		}
		//小区名称
		if e.Address != "" {
			table = table.Where("`order_student`.student_addr like ?", "%"+e.Address+"%")
		}
	}
	if e.ShareType != "" {
		table = table.Where("`order_share`.share_type =  ?", e.ShareType)

	} else {
		table = table.Where("`order_share`.share_type in (2,3)")

	}

	table = table.Where("`order_share`.share_price > 0")

	if e.ShareStatus != "" {
		table = table.Where("`order_share`.share_status = ?", e.ShareStatus)
	}
	//分佣金时间
	if e.ShareTimeStart != "" {
		table = table.Where("`order_share`.share_time >= ?", e.ShareTimeStart)
	}
	if e.ShareTimeEnd != "" {
		table = table.Where("`order_share`.share_time <= ?", e.ShareTimeEnd)
	}

	if e.ShareTimeMonth != "" {
		table = table.Where("`order_share`.share_time >= ?", e.ShareTimeMonth+"-00 00:00:00")
		table = table.Where("`order_share`.share_time <= ?", e.ShareTimeMonth+"-31 23:59:59")
	}

	// ---- //
	if e.OrderId != 0 {
		table = table.Where("`order`.order_id = ?", e.OrderId)
	}
	if e.OrderNo != "" {
		table = table.Where("`order`.order_no like ?", "%"+e.OrderNo+"%")
	}
	if e.OrderName != "" {
		table = table.Where("`order`.order_name like ?", "%"+e.OrderName+"%")
	}
	if e.BrandName != "" {
		table = table.Where("`order`.brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.OrganName != "" {
		table = table.Where("`order`.organ_name like ?", "%"+e.OrganName+"%")
	}

	if e.OrderStatus != "" {
		table = table.Where("`order`.order_status = ?", e.OrderStatus)
	}
	//MemberMomId
	if e.MemberMomId != 0 {
		table = table.Where("`order`.member_mom_id = ?", e.MemberMomId)
	}
	if e.MemberId != 0 {
		table = table.Where("`order`.member_id = ?", e.MemberId)
	}
	if e.MemberName != "" {
		table = table.Where("`order`.member_name like ?", "%"+e.MemberName+"%")
	}
	if e.MemberMomName != "" {
		table = table.Where("`order`.member_mom_name like ?", "%"+e.MemberMomName+"%")
	}
	if e.MemberPhone != "" {
		table = table.Where("`order`.member_phone like ?", "%"+e.MemberPhone+"%")
	}

	if e.OrganId != 0 {
		table = table.Where("`order`.organ_id = ?", e.OrganId)
	}
	if e.PayType != "" {
		table = table.Where("`order`.pay_type = ?", e.PayType)
	}
	if e.PayNo != "" {
		table = table.Where("`order`.pay_no like ?", "%"+e.PayNo+"%")
	}
	if e.PayTime != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTime)
	}
	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("`order`.updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("`order`.updated_at <= ?", e.UpdateEnd)
	}

	//支付时间
	if e.PayTimeStart != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTimeStart)
	}
	if e.PayTimeEnd != "" {
		table = table.Where("`order`.pay_time <= ?", e.PayTimeEnd)
	}

	// 数据权限
	table, _ = HandleOrganCommonTable(table, "order", e.DataScope, "分佣金列表") // 分佣金列表

	var count int
	if err := table.Order("`order`.order_id desc", true).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`order`.`deleted_at` IS NULL").Count(&count)

	var courseIn Course
	var organIn Organ
	var member Member
	//var studentIn OrderStudent
	//var shareIn OrderShare
	//处理订单信息
	for i := 0; i < len(doc); i++ {
		ii := doc[i]
		//获取课程信息
		doc[i].CourseInfo, _ = courseIn.GetById(ii.CourseId)

		//获取机构信息
		doc[i].OrganInfo, _ = organIn.GetByID(ii.OrganId)

		if ii.ShareType == "2" || ii.ShareType == "3" {
			doc[i].MemberMomName = member.GetMomName(ii.ShareUserId)
			doc[i].MemberMomId = ii.ShareUserId
		} else {
			doc[i].MemberMomName = ""
			doc[i].MemberMomId = 0
		}
	}

	return doc, count, nil
}

// 财务分红列表 GetSharePage
func (e *Order) GetSharePage(pageSize int, pageIndex int) ([]OrderShareList, int, error) {
	var doc []OrderShareList

	table := orm.Eloquent.Select(" `order`.* , `order_share`.* ").Table(e.TableName())
	table = table.Joins("inner join `order_share` on `order_share`.order_id = `order`.order_id")

	//使用 课程id
	if e.CourseId != 0 || e.CourseName != "" {
		if e.CourseId != 0 {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id and `order_course`.course_id = ?", e.CourseId)
		} else if e.CourseName != "" {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id ")
			table = table.Where("`order_course`.course_name like ?", "%"+e.CourseName+"%")
		}
	}

	table = table.Joins("inner join `order_student` on `order_student`.order_id = `order`.order_id ")

	//使用到 order_student
	if e.AddressSearch != "" || e.Address != "" {

		//所在地区
		if e.AddressSearch != "" {
			table = table.Where("`order_student`.student_addr like ?", "%"+e.AddressSearch+"%")
		}
		//小区名称
		if e.Address != "" {
			table = table.Where("`order_student`.student_addr like ?", "%"+e.Address+"%")
		}
	}

	if e.ShareType != "" {
		table = table.Where("`order_share`.share_type = ?", e.ShareType)
	}
	if e.ShareStatus != "" {
		table = table.Where("`order_share`.share_status = ?", e.ShareStatus)
	}
	//分佣金时间
	if e.ShareTimeStart != "" {
		table = table.Where("`order_share`.share_time >= ?", e.ShareTimeStart)
	}
	if e.ShareTimeEnd != "" {
		table = table.Where("`order_share`.share_time <= ?", e.ShareTimeEnd)
	}

	if e.ShareTimeMonth != "" {
		table = table.Where("`order_share`.share_time >= ?", e.ShareTimeMonth+"-00 00:00:00")
		table = table.Where("`order_share`.share_time <= ?", e.ShareTimeMonth+"-31 23:59:59")
	}

	// ---- //
	if e.OrderId != 0 {
		table = table.Where("`order`.order_id = ?", e.OrderId)
	}
	if e.OrderNo != "" {
		table = table.Where("`order`.order_no like ?", "%"+e.OrderNo+"%")
	}
	if e.OrderName != "" {
		table = table.Where("`order`.order_name like ?", "%"+e.OrderName+"%")
	}
	if e.BrandName != "" {
		table = table.Where("`order`.brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.OrganName != "" {
		table = table.Where("`order`.organ_name like ?", "%"+e.OrganName+"%")
	}

	if e.OrderStatus != "" {
		table = table.Where("`order`.order_status = ?", e.OrderStatus)
	}
	//MemberMomId
	if e.MemberMomId != 0 {
		table = table.Where("`order`.member_mom_id = ?", e.MemberMomId)
	}
	if e.MemberId != 0 {
		table = table.Where("`order`.member_id = ?", e.MemberId)
	}
	if e.MemberName != "" {
		table = table.Where("`order`.member_name like ?", "%"+e.MemberName+"%")
	}
	if e.MemberMomName != "" {
		table = table.Where("`order`.member_mom_name like ?", "%"+e.MemberMomName+"%")
	}
	if e.MemberPhone != "" {
		table = table.Where("`order`.member_phone like ?", "%"+e.MemberPhone+"%")
	}

	if e.OrganId != 0 {
		table = table.Where("`order`.organ_id = ?", e.OrganId)
	}
	if e.PayType != "" {
		table = table.Where("`order`.pay_type = ?", e.PayType)
	}
	if e.PayNo != "" {
		table = table.Where("`order`.pay_no like ?", "%"+e.PayNo+"%")
	}
	if e.PayTime != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTime)
	}
	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("`order`.updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("`order`.updated_at <= ?", e.UpdateEnd)
	}

	//支付时间
	if e.PayTimeStart != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTimeStart)
	}
	if e.PayTimeEnd != "" {
		table = table.Where("`order`.pay_time <= ?", e.PayTimeEnd)
	}

	// 数据权限
	table, _ = HandleOrganCommonTable(table, "order", e.DataScope, "分佣金列表") // 分佣金列表

	var count int
	if err := table.Order("`order`.order_id desc", true).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`order`.`deleted_at` IS NULL").Count(&count)

	var courseIn Course
	var organIn Organ
	//var studentIn OrderStudent
	//var shareIn OrderShare
	//处理订单信息
	for i := 0; i < len(doc); i++ {
		ii := doc[i]
		//获取课程信息
		doc[i].CourseInfo, _ = courseIn.GetById(ii.CourseId)

		//获取机构信息
		doc[i].OrganInfo, _ = organIn.GetByID(ii.OrganId)

	}

	return doc, count, nil
}

// 用户侧
func (e *Order) GetMemberPage(pageSize int, pageIndex int) ([]Order, int, error) {
	var doc []Order

	table := orm.Eloquent.Select(" `order`.* ").Table(e.TableName())
	if e.CourseId != 0 || e.CourseName != "" {
		if e.CourseId != 0 {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id and `order_course`.course_id = ?", e.CourseId)
		} else if e.CourseName != "" {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id ")
			table = table.Where("`order_course`.course_name like ?", "%"+e.CourseName+"%")
		}
	}
	if e.OrderId != 0 {
		table = table.Where("order_id = ?", e.OrderId)
	}
	if e.OrderNo != "" {
		table = table.Where("order_no like ?", "%"+e.OrderNo+"%")
	}
	if e.OrderName != "" {
		table = table.Where("order_name like ?", "%"+e.OrderName+"%")
	}
	if e.BrandName != "" {
		table = table.Where("brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.OrganName != "" {
		table = table.Where("organ_name like ?", "%"+e.OrganName+"%")
	}

	table = table.Where("order_status >= ?", 5) // m默认大于 5可见
	if e.StatusType != "" {
		if e.StatusType == "1" {
			table = table.Where("order_status = ?", 5)

		} else if e.StatusType == "5" {
			table = table.Where("order_status >= ? AND order_status <= ?", 10, 80)

		} else if e.StatusType == "10" {
			table = table.Where("order_status in (20,90,80)")
		}
	} else {
		if e.OrderStatus != "" {
			table = table.Where("order_status = ?", e.OrderStatus)
		}
	}
	//MemberMomId
	if e.MemberMomId != 0 {
		table = table.Where("member_mom_id = ?", e.MemberMomId)
	}
	if e.MemberId != 0 {
		table = table.Where("member_id = ?", e.MemberId)
	}
	if e.MemberName != "" {
		table = table.Where("member_name like ?", "%"+e.MemberName+"%")
	}
	if e.MemberMomName != "" {
		table = table.Where("member_mom_name like ?", "%"+e.MemberMomName+"%")
	}
	if e.MemberPhone != "" {
		table = table.Where("member_phone like ?", "%"+e.MemberPhone+"%")
	}

	if e.OrganId != 0 {
		table = table.Where("organ_id = ?", e.OrganId)
	}
	if e.PayType != "" {
		table = table.Where("pay_type = ?", e.PayType)
	}
	if e.PayNo != "" {
		table = table.Where("pay_no like ?", "%"+e.PayNo+"%")
	}
	if e.PayTime != "" {
		table = table.Where("pay_time >= ?", e.PayTime)
	}
	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("updated_at <= ?", e.UpdateEnd)
	}

	var count int
	if err := table.Order("`order`.order_id desc", true).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`order`.`deleted_at` IS NULL").Count(&count)

	var courseIn Course
	var organIn Organ
	var studentIn OrderStudent
	//处理订单信息
	for i := 0; i < len(doc); i++ {
		ii := doc[i]
		//获取课程信息
		doc[i].CourseInfo, _ = courseIn.GetById(ii.CourseId)

		//获取机构信息
		doc[i].OrganInfo, _ = organIn.GetByID(ii.OrganId)

		//学生信息
		doc[i].StudentInfo, _ = studentIn.GetById(ii.OrderId)

	}

	return doc, count, nil
}

// GetList
func (e *Order) GetMomList(pageSize int, pageIndex int, momid int) ([]Order, int, error) {
	var doc []Order

	table := orm.Eloquent.Select(" * ").Table(e.TableName())
	table = table.Where("member_mom_id = ?", momid)
	table = table.Where("order_status = ? OR order_status = ? ", 20, 90)
	// orderStatus
	//订单状态1 进行中（创建）
	//支付状态
	//5  待付款     用户下单后 未完成支付支付完成
	//10 已支付 （- 订单可以申请退款-）
	//20 已确认 （用户确认订单- 订单可以申请退款  / 或者 系统15天自动确认）   -- （ 进行分账给机构 ）退款状态
	//60 用户申请退款/待核实  （前台触发 微信端用户点击 ）
	//65 审核不通过/退款申请失败（订单可以申请退款）   --（后台点击退款审核 不通过）
	//70 退款申请成功/ 待退款                                             -- （后台点击退款审核通过）
	//80 全部退款 完成 （此动作 后台触发 将进行 微信订单退款操作）订单结束状态
	// 90  已完成 （此状态下 订单不能申请退款  / 订单已经完成排课 且到期）
	// 91 已关闭 （/已退款完成 周期7天完成关闭）
	// 100 订单完成 所有分账 佣金
	var count int
	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where(" `deleted_at` IS NULL").Count(&count)

	return doc, count, nil
}

// 获取Order带分页
func (e *Order) GetFiancePage(pageSize int, pageIndex int) ([]OrderFiance, int, error) {
	var doc []OrderFiance

	ss := "`order`.* ,`organ`.organ_money_type,`order_student`.student_addr,`order_student`.student_address ,`order_share`.share_status,`order_share`.share_time,`order_share`.share_type,`order_share`.share_mom_organ_price,`order_share`.share_mom_member_price "
	table := orm.Eloquent.Select(ss).Table(e.TableName())
	if e.CourseId != 0 || e.CourseName != "" {
		if e.CourseId != 0 {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id and `order_course`.course_id = ?", e.CourseId)
		} else if e.CourseName != "" {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id ")
			table = table.Where("`order_course`.course_name like ?", "%"+e.CourseName+"%")
		}
	}

	table = table.Joins("inner join `order_student` on `order_student`.order_id = `order`.order_id ")
	table = table.Joins("left join `organ` on `organ`.organ_id = `order`.organ_id ")
	table = table.Joins("left join `order_share` on `order_share`.order_id = `order`.order_id and `order_share`.share_type = 1 and share_to = 0")

	//使用到 order_student
	if e.AddressSearch != "" || e.Address != "" {

		//所在地区
		if e.AddressSearch != "" {
			table = table.Where("`order_student`.student_addr like ?", "%"+e.AddressSearch+"%")
		}
		//小区名称
		if e.Address != "" {
			table = table.Where("`order_student`.student_addr like ?", "%"+e.Address+"%")
		}
	}

	if e.OrderId != 0 {
		table = table.Where("`order`.order_id = ?", e.OrderId)
	}
	if e.OrderNo != "" {
		table = table.Where("`order`.order_no like ?", "%"+e.OrderNo+"%")
	}

	if e.IsShare != "" {
		table = table.Where("`order`.is_share = ?", e.IsShare)
	}
	//支付时间
	if e.ShareTimeStart != "" {
		table = table.Where("`order`.share_time >= ?", e.ShareTimeStart)
	}
	if e.ShareTimeEnd != "" {
		table = table.Where("`order`.share_time <= ?", e.ShareTimeEnd)
	}

	if e.IsSure != "" {
		table = table.Where("`order`.is_sure = ?", e.IsSure)
	}

	//支付时间
	if e.SureTimeStart != "" {
		table = table.Where("`order`.sure_time >= ?", e.SureTimeStart)
	}
	if e.SureTimeEnd != "" {
		table = table.Where("`order`.sure_time <= ?", e.SureTimeEnd)
	}

	if e.OrderName != "" {
		table = table.Where("`order`.order_name like ?", "%"+e.OrderName+"%")
	}
	if e.BrandName != "" {
		table = table.Where("`order`.brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.OrganName != "" {
		table = table.Where("`order`.organ_name like ?", "%"+e.OrganName+"%")
	}
	if e.CourseName != "" {
		table = table.Where("`order`.course_name like ?", "%"+e.CourseName+"%")
	}
	if e.OrganId != 0 {
		table = table.Where("`order`.organ_id = ?", e.OrganId)
	}
	if e.CourseId != 0 {
		table = table.Where("`order`.course_id = ?", e.CourseId)
	}
	if e.OrderStatus != "" {
		table = table.Where("`order`.order_status = ?", e.OrderStatus)
	}
	//MemberMomId
	if e.MemberMomId != 0 {
		table = table.Where("`order`.member_mom_id = ?", e.MemberMomId)
	}
	if e.MemberId != 0 {
		table = table.Where("`order`.member_id = ?", e.MemberId)
	}
	if e.MemberName != "" {
		table = table.Where("`order`.member_name like ?", "%"+e.MemberName+"%")
	}
	if e.MemberMomName != "" {
		table = table.Where("`order`.member_mom_name like ?", "%"+e.MemberMomName+"%")
	}
	if e.MemberPhone != "" {
		table = table.Where("`order`.member_phone like ?", "%"+e.MemberPhone+"%")
	}

	if e.OrganId != 0 {
		table = table.Where("`order`.organ_id = ?", e.OrganId)
	}
	if e.PayType != "" {
		table = table.Where("`order`.pay_type = ?", e.PayType)
	}
	if e.PayNo != "" {
		table = table.Where("`order`.pay_no like ?", "%"+e.PayNo+"%")
	}
	if e.PayTime != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTime)
	}
	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("`order`.updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("`order`.updated_at <= ?", e.UpdateEnd)
	}

	//支付时间
	if e.PayTimeStart != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTimeStart)
	}
	if e.PayTimeEnd != "" {
		table = table.Where("`order`.pay_time <= ?", e.PayTimeEnd)
	}

	// 数据权限
	table, _ = HandleOrganCommonTable(table, "order", e.DataScope, "财务机构列表") // 分佣金列表

	var count int
	if err := table.Order("`order`.order_id desc", true).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`order`.`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 获取Order带分页
func (e *Order) GetListCreat(orderStatus int, createTime string) ([]Order, error) {
	var doc []Order

	table := orm.Eloquent.Select(" `order`.*").Table(e.TableName())
	if orderStatus > 0 {
		table = table.Where("`order`.order_status = ?", orderStatus)
	}
	if createTime != "" {
		table = table.Where("`order`.create_time <= ?", createTime)
	}

	if err := table.Order("`order`.order_id desc", true).Limit(20).Find(&doc).Error; err != nil {
		return nil, err
	}
	return doc, nil
}

func (e *Order) GetListPay(orderStatus int, payTime string) ([]Order, error) {
	var doc []Order

	table := orm.Eloquent.Select(" `order`.*").Table(e.TableName())
	if orderStatus > 0 {
		table = table.Where("`order`.order_status = ?", orderStatus)
	}

	if payTime != "" {
		table = table.Where("`order`.pay_time <= ?", payTime)
	}

	if err := table.Order("`order`.order_id desc", true).Limit(20).Find(&doc).Error; err != nil {
		return nil, err
	}
	return doc, nil
}

// 获取Order带分页
func (e *Order) GetPage(pageSize int, pageIndex int) ([]Order, int, error) {
	var doc []Order

	table := orm.Eloquent.Select(" `order`.* ,`order_student`.student_addr ").Table(e.TableName())
	if e.CourseId != 0 || e.CourseName != "" {
		if e.CourseId != 0 {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id and `order_course`.course_id = ?", e.CourseId)
		} else if e.CourseName != "" {
			table = table.Joins("right join `order_course` on `order_course`.order_id = `order`.order_id ")
			table = table.Where("`order_course`.course_name like ?", "%"+e.CourseName+"%")
		}
	}

	table = table.Joins("inner join `order_student` on `order_student`.order_id = `order`.order_id ")

	//使用到 order_student
	if e.AddressSearch != "" || e.Address != "" {

		//所在地区
		if e.AddressSearch != "" {
			table = table.Where("`order_student`.student_addr like ?", "%"+e.AddressSearch+"%")
		}
		//小区名称
		if e.Address != "" {
			table = table.Where("`order_student`.student_addr like ?", "%"+e.Address+"%")
		}
	}

	if e.OrderId != 0 {
		table = table.Where("`order`.order_id = ?", e.OrderId)
	}
	if e.OrderNo != "" {
		table = table.Where("`order`.order_no like ?", "%"+e.OrderNo+"%")
	}
	if e.OrderName != "" {
		table = table.Where("`order`.order_name like ?", "%"+e.OrderName+"%")
	}
	if e.BrandName != "" {
		table = table.Where("`order`.brand_name like ?", "%"+e.BrandName+"%")
	}
	if e.OrganName != "" {
		table = table.Where("`order`.organ_name like ?", "%"+e.OrganName+"%")
	}
	if e.OrderStatus != "" {
		table = table.Where("`order`.order_status = ?", e.OrderStatus)
	}
	//MemberMomId
	if e.MemberMomId != 0 {
		table = table.Where("`order`.member_mom_id = ?", e.MemberMomId)
	}
	if e.MemberId != 0 {
		table = table.Where("`order`.member_id = ?", e.MemberId)
	}
	if e.MemberName != "" {
		table = table.Where("`order`.member_name like ?", "%"+e.MemberName+"%")
	}
	if e.MemberMomName != "" {
		table = table.Where("`order`.member_mom_name like ?", "%"+e.MemberMomName+"%")
	}
	if e.MemberPhone != "" {
		table = table.Where("`order`.member_phone like ?", "%"+e.MemberPhone+"%")
	}

	if e.OrganId != 0 {
		table = table.Where("`order`.organ_id = ?", e.OrganId)
	}
	if e.PayType != "" {
		table = table.Where("`order`.pay_type = ?", e.PayType)
	}
	if e.PayNo != "" {
		table = table.Where("`order`.pay_no like ?", "%"+e.PayNo+"%")
	}
	if e.PayTime != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTime)
	}
	//data.UpdateStart
	if e.UpdateStart != "" {
		table = table.Where("`order`.updated_at >= ?", e.UpdateStart)
	}
	if e.UpdateEnd != "" {
		table = table.Where("`order`.updated_at <= ?", e.UpdateEnd)
	}

	//支付时间
	if e.PayTimeStart != "" {
		table = table.Where("`order`.pay_time >= ?", e.PayTimeStart)
	}
	if e.PayTimeEnd != "" {
		table = table.Where("`order`.pay_time <= ?", e.PayTimeEnd)
	}

	// 数据权限
	table, _ = HandleOrganCommonTable(table, "order", e.DataScope, "订单列表") // 分佣金列表

	var count int
	if err := table.Order("`order`.order_id desc", true).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`order`.`deleted_at` IS NULL").Count(&count)

	var courseIn Course
	var organIn Organ
	var studentIn OrderStudent
	var shareIn OrderShare
	//处理订单信息
	for i := 0; i < len(doc); i++ {
		ii := doc[i]
		//获取课程信息
		doc[i].CourseInfo, _ = courseIn.GetById(ii.CourseId)

		//获取机构信息
		doc[i].OrganInfo, _ = organIn.GetByID(ii.OrganId)

		//学生信息
		studentIn.OrderId = ii.OrderId
		doc[i].StudentInfo, _ = studentIn.Get()

		//分红信息
		shareIn.OrderId = ii.OrderId
		doc[i].ShareInfo = shareIn.GetList()
	}

	return doc, count, nil
}

// UpdateSure
func (e *Order) UpdateSure(orderId, organId int) error {
	table := orm.Eloquent.Table(e.TableName()).Where("order_id = ?", orderId).Where("organ_id=?", organId)
	table.Update("is_share", 1)

	return table.Error
}

// 更新Order
func (e *Order) Update(id int) (update Order, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

func (e *Order) Close(id int, change OrderClose) (update Order, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&change).Error; err != nil {
		return
	}
	return
}

// 删除Order
func (e *Order) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_id = ?", id).Delete(&Order{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *Order) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("order_id in (?)", id).Delete(&Order{}).Error; err != nil {
		return
	}
	Result = true
	return
}
